<template>
  <div class="banner-container">
    <div class="button-box">
      <el-button type="primary" @click="$router.push({ name: 'addbanner' })">
        添加轮播图<el-icon class="el-icon--right"><Plus /></el-icon>
      </el-button>
      <el-button type="primary" @click="handleExcelExport">
        导出excel<el-icon><Download /></el-icon>
      </el-button>
    </div>
    <el-table
      id="Table2XLSX"
      :data="computedTableData"
      :header-cell-style="headerRowStyle"
      :cell-style="{ textAlign: 'center', height: '80px' }"
      max-height="960"
      style="width: 100%"
    >
      <el-table-column type="index" label="序号" width="80" />
      <el-table-column prop="img" label="图片">
        <template #default="scope">
          <el-image style="height: 100px" :src="scope.row.url" alt="" />
        </template>
      </el-table-column>
      <el-table-column prop="url" label="链接"></el-table-column>
      <el-table-column prop="content" label="提示"></el-table-column>
      <el-table-column label="操作">
        <template #default="scope">
          <el-popconfirm
            title="确认删除吗？"
            confirm-button-text="确认"
            cancel-button-text="取消"
            @confirm="handleDelete(scope.row.id)"
          >
            <template #reference>
              <el-button size="small" type="danger">Delete</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total, prev, pager, next"
      v-model:current-page="currentPage"
      :total="tableData.length"
    />
  </div>
</template>

<script>
import { defineComponent } from 'vue'
import { getBannerList } from '@/api/banner'
import { Plus, Download } from '@element-plus/icons-vue'
import { excelExport, excelExport2 } from '@/utils'

export default {
  data() {
    return {
      headerRowStyle: {
        color: 'rgba(0, 0, 0, .85)',
        fontWeight: '700',
        fontSize: '14px',
        textAlign: 'center',
        background: '#f8f8f8',
        height: '60px',
        borderRight: '1px solid #f0f0f0',
        transition: 'background .3s ease',
      },
      tableData: [],
      currentPage: 1,
    }
  },
  components: {
    Plus,
    Download,
  },
  methods: {
    // 表格内容导出为excel
    handleExcelExport() {
      /* Create worksheet from HTML DOM TABLE */
      // 方式1： 使用dom元素来导出
      // const table = document.getElementById('Table2XLSX')
      // excelExport(table, '轮播图列表')
      // 方式2： 使用数据导出excel
      // this.computedTableData
      // 1.设置excel的表头 2. 传递数据 3.导出excel文件的名字
      excelExport2(
        this.computedTableData,
        {
          url: '图片',
          content: '提示',
          url1: '链接',
        },
        '轮播图列表'
      )
    },
    refresh() {
      this.currentPage = 1
      this.getTableList()
    },
    // 获取轮播图列表数据
    getTableList() {
      getBannerList().then((res) => {
          console.log(res)
        this.tableData = res.resultInfo.list
      })
    },
    handleDelete(bannerid) {
      deleteBanner({ bannerid }).then((res) => {
        if (res.code === '200') {
          this.refresh()
        }
      })
    },
  },
  computed: {
    computedTableData() {
      return this.tableData.slice(
        (this.currentPage - 1) * 10,
        (this.currentPage - 1) * 10 + 10
      )
    },
  },
  mounted() {
    this.getTableList()
  },
}
</script>

<style lang="scss" scoped>
.banner-container {
  height: 100%;
  background-color: #fff;
  padding: 24px;
  .button-box {
    margin-bottom: 24px;
  }
  .el-pagination {
    justify-content: flex-end;
    margin-top: 32px;
  }
}
</style>
